<?php
class Server {
  public static function cancel($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    if (isset($_SESSION['user'])) {
      $user = json_decode($_SESSION['user']);
      if ($user->id == $req->customer_id) {
        $orm->update('pb_ecom_order', $req);
        require_once '../include/ecomutils.class.php';
        EComUtils::update_customer($orm, $req);
        $opts = new stdClass();
        $opts->cd = 't.order_id = :v1';
        $opts->cdv = array(':v1' => $req->id);
        $rslt = $orm->get('pb_ecom_purchase', NULL, NULL, NULL, NULL, $opts)->d;
        foreach ($rslt as $itm) {
          EComUtils::update_stock($orm, $itm, 1);
        }
      }
    }
  }
  
  public static function submit_obj($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    if (isset($_SESSION['user'])) {
      $user = json_decode($_SESSION['user']);
      $user->first_name = $req->first_name;
      $user->last_name = $req->last_name;
      $user->phone = $req->phone;
      $orm->update('pb_ecom_customer', $user);
      $_SESSION['user'] = json_encode($user);
    }
  }
  
  public static function set_pass($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    if (isset($_SESSION['user'])) {
      $user = json_decode($_SESSION['user']);
      
      $opts = new stdClass();
      $opts->cd = 't.id = :v1';
      $opts->cdv = array(':v1' => $user->id);
      $rslt = $orm->get('pb_ecom_customer', NULL, NULL, NULL, NULL, $opts)->d;
      if (count($rslt) < 0) {
        echo '用户不存在';
        return;
      }
      
      if (md5($user->salt . $req->oldpass) != $rslt[0]->password) {
        echo '老密码不正确';
        return;
      }
      
      if (strlen($req->pass) < 6 || strlen($req->pass) > 16) {
        echo '密码必须6-16位';
        return;
      }
      if ($req->pass != $req->retype) {
        echo '密码不一致';
        return;
      }
      
      
      $user->password = md5($user->salt . $req->pass);
      $orm->update('pb_ecom_customer', $user);
      unset($user->password);
      $_SESSION['user'] = json_encode($user);
    }
  }
  
  public static function delete_addr($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    $orm->delete('pb_ecom_customer_addr', $req);

    if (isset($_SESSION['user'])) {
      $user = json_decode($_SESSION['user']);
      foreach ($user->__addrs as $idx => $itm) {
        if ($itm->id == $req->id) {
          array_splice($user->__addrs, $idx, 1);
        }
      }
      
      if ($req->is_default == 1) {
        $opts = new stdClass();
        $opts->cd = 't.customer_id = :v1';
        $opts->cdv = array(':v1' => $req->customer_id);
        $rslt = $orm->get('pb_ecom_customer_addr', NULL, NULL, NULL, NULL, $opts)->d;
        if (count($rslt) > 0) {
          $rslt[0]->is_default = 1;
          $orm->update('pb_ecom_customer_addr', $rslt[0]);
        }
      }
      
      $_SESSION['user']= json_encode($user);
    }
  }
  
  public static function set_addr($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    if ($req->first_name == '' || 
        $req->last_name == '' || 
        $req->address1 == '' || 
        $req->city == '' || 
        $req->country == '' || 
        $req->zip == '' || 
        $req->phone == '' || 
        $req->email == '' ) {
      echo '送货地址不完整';
      return;
    }
    
    if (isset($_SESSION['user'])) {
      $user = json_decode($_SESSION['user']);
      if (isset($req->id) && $req->id != 0) {
        if ($req->is_default == 1) {
          $orm->exec('UPDATE pb_ecom_customer_addr SET is_default = 0 WHERE customer_id = ' . $user->id);
        }
        $orm->update('pb_ecom_customer_addr', $req);
      } else {
        $req->customer_id = $user->id;
        $orm->add('pb_ecom_customer_addr', $req);
        $req->id = $orm->last_id();
        echo json_encode($req);
      }
    
      $ex = 0;
      foreach ($user->__addrs as $idx => $itm) {
        if ($itm->id == $req->id) {
          $user->__addrs[$idx] = $req;
          $ex = 1;
        }
      }
      if ($ex == 0) {
        $user->__addrs[] = $req;
      }
      $_SESSION['user']= json_encode($user);
    }
  }
}
?>